package com.bdzl.flow.controller;

import com.bdzl.flow.service.ActModelService;
import com.bdzl.flow.service.ActNodeService;
import com.bdzl.pub.controller.BaseController;
import com.bdzl.pub.entity.flow.ActModelDO;
import com.bdzl.pub.entity.sys.UserDO;
import com.bdzl.pub.util.param.PageUtils;
import com.bdzl.pub.util.param.Query;
import com.bdzl.pub.util.param.R;
import com.bdzl.pub.vo.flow.ActModelVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工作流模型信息表
 *
 * @author yangyicai
 * @email yangyicai@foxmail.com
 * @date 2021-05-12 10:14:58
 */

@Controller
@RequestMapping("/bdzl/actModel")
public class ActModelController extends BaseController {
	@Autowired
	private ActModelService actModelService;
	@Autowired
	private ActNodeService actNodeService;

	/**
	* @Description: TODO 工作流列表数据
	* @param params
	* @return: com.bdzl.pub.util.param.PageUtils
	* @Author: Ming
	* @Date: 2021/5/14 14:43
	*/
	@ResponseBody
	@PostMapping("/list")
	public PageUtils list(@RequestBody Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<ActModelVO> actModelList = actModelService.list(query);
		int total = actModelService.count(query);
		PageUtils pageUtils = new PageUtils(actModelList, total);
		return pageUtils;
	}

	/**
	* @Description: TODO 根据id查询
	* @param actId id
	* @return: com.bdzl.pub.util.param.R
	* @Author: Ming
	* @Date: 2021/5/14 14:49
	*/
	@GetMapping("/edit/{actId}")
	@ResponseBody
	public R edit(@PathVariable("actId") Integer actId){
		ActModelVO actModel = actModelService.get(actId);
	    return R.ok(actModel);
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save(@RequestBody ActModelDO actModel,HttpServletRequest request){
		//获取当前登录用户数据
		UserDO userInfo = actNodeService.getUserInfo(request);
		actModel.setActCreuser(userInfo.getUserId());
		actModel.setActCretime(new Date());
		if(actModelService.save(actModel)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update(@RequestBody ActModelDO actModel,HttpServletRequest request){
		//获取当前登录用户数据
		UserDO userInfo = actNodeService.getUserInfo(request);
		actModel.setActModuser(userInfo.getUserId());
		actModel.setActModtime(new Date());
		actModelService.update(actModel);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove(@RequestBody Map<String, Object> params){
		Object actId = params.get("actId");
		if(actModelService.remove((Integer) actId)>0){
		return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Integer[] actIds){
		actModelService.batchRemove(actIds);
		return R.ok();
	}
	/**
	* @Description: TODO 获取所有工作流数据
	* @return: java.lang.String
	* @Author: Ming
	* @Date: 2021/5/14 13:59
	*/
	@GetMapping("/getModel")
	@ResponseBody
	public R getModel(){
		List<ActModelVO> actModelList = actModelService.list(new HashMap<>());
		return R.ok(actModelList);
	}
}
